#include "legendwidget.h"
#include <QMapIterator>
#include <QList>
#include "curvedata.h"
#include "abstractplot2d.h"
#include <QPainter>

LegendWidget::LegendWidget(QWidget *parent) :
    QWidget(parent)
{
    _plotter = static_cast<AbstractPlot2D *>(parent);
    setMouseTracking(true);

    _opacity = 200;
    _showFrame = true;
}
//-----------------------------------------------------------------------------

void LegendWidget::setOpacity(int opacity)
{
    if (opacity != _opacity) {
        _opacity = opacity;
        update();
    }
}
//-----------------------------------------------------------------------------

int LegendWidget::opacity() const
{
    return _opacity;
}
//-----------------------------------------------------------------------------

bool LegendWidget::frameIsShow() const
{
    return _showFrame;
}
//-----------------------------------------------------------------------------

void LegendWidget::showFrame(bool isShow)
{
    if (_showFrame != isShow) {
        _showFrame = isShow;
        update();
    }
}
//-----------------------------------------------------------------------------

QSize LegendWidget::sizeHint() const
{
    int w = 2 * Margin;
    int h = 2 * Margin;

    if (_plotter != 0) {
        QListIterator<bool> showCurvesListIt(_plotter->showCurvesList());
        QListIterator<CurveData> curvesListIt(_plotter->curvesList());
        _label.setFont(this->font());
        _label.adjustSize();
        int num = 0;
        int maxWidth = 0;
        while (curvesListIt.hasNext()) {
            const CurveData &cd = curvesListIt.next();
            const bool &show = showCurvesListIt.next();
            if (show) {
                _label.setText(cd.legend());
                _label.adjustSize();
                if (_label.width() >= maxWidth)
                    maxWidth = _label.width();
                h += _label.height();
                num++;
            }
        }

        if (maxWidth > 0)
            w += (maxWidth + Margin + MarkerSize);
        if (num > 1) {
            h += (num-1)*Margin;
        }
    }

    return QSize(w, h);
}
//-----------------------------------------------------------------------------

QSize LegendWidget::minimumSizeHint() const
{
    return sizeHint();
}
//-----------------------------------------------------------------------------

void LegendWidget::paintEvent(QPaintEvent *event)
{
    adjustSize();
    QPainter painter(this);
    QPen pen(Qt::black);
    if (_showFrame)
        pen.setStyle(Qt::SolidLine);
    else
        pen.setStyle(Qt::NoPen);

    QBrush brush(QColor(255, 255, 255, _opacity));
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawRect(rect().adjusted(0, 0, -1, -1));

    QPalette p = palette();
    p.setColor(QPalette::Window, Qt::white);
    p.setColor(QPalette::WindowText, Qt::black);
    _label.setPalette(p);
    _label.setFont(this->font());
    _label.adjustSize();
    if (_plotter != 0) {
        QListIterator<bool> showCurvesListIt(_plotter->showCurvesList());
        QListIterator<CurveData> curvesListIt(_plotter->curvesList());
        int _x = Margin;
        int _y = Margin;
        int num = 0;
        while (curvesListIt.hasNext()) {
            const CurveData &cd = curvesListIt.next();
            const bool &show = showCurvesListIt.next();

            if (show) {
                _label.setText(cd.legend());
                _label.adjustSize();

                if (num == 0)
                    _y += _label.height()/2;
                else
                    _y += (_label.height() + Margin);

                num++;
                pen.setWidth(cd.penWidth());
                pen.setColor(cd.color());
                pen.setStyle(cd.penStyle());
                painter.setPen(pen);
                painter.drawLine(_x, _y, _x+MarkerSize, _y);
                brush.setColor(cd.color());
                painter.setBrush(brush);
                painter.setRenderHint(QPainter::Antialiasing, true);
                _plotter->drawSymbol(&painter, cd.symbol(),
                                    cd.symbolSize(),
                                    QPointF(_x+MarkerSize/2, _y),
                                    cd.color());
                painter.setRenderHint(QPainter::Antialiasing, false);

                QPixmap _pixmap(_label.size());
                _label.render(&_pixmap);
                QImage img(_pixmap.size(), QImage::Format_ARGB32);
                QImage img2 = _pixmap.toImage();
                QRgb clr0 = img2.pixel(0, 0);

                for (int n = 0; n < img.width(); ++n) {
                    for (int m = 0; m < img.height(); ++m) {
                        QRgb clr = img2.pixel(n, m);
                        if (clr == clr0) {
                            clr = qRgba(255, 255, 255, 0);
                        }
                        img.setPixel(n, m, clr);
                    }
                }
                painter.drawImage(QPoint(_x+MarkerSize+Margin, _y-_label.height()/2), img);
            }
        }
    }

    QWidget::paintEvent(event);
}
//-----------------------------------------------------------------------------
